var setting = {
      check: {  
            enable: true,                           //设置 zTree 的节点上是否显示 checkbox / radio  
            chkStyle:"checkbox",                    //勾选框类型(checkbox 或 radio）   
            chkboxType: { "Y": "ps", "N": "ps" },    //父子级联关系  
            radioType: "all"
         },  
      data: {  
    	  simpleData: {
				enable: true,
				idKey: "REC_ID",
				pIdKey: "PARENT_ID"
			},
			key: {
				name: "MODULE_NAME"
			}
         },
      callback: {
    	  beforeClick: beforeClick,
			onCheck: onCheck
          }  
};
function beforeClick(treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo");
	zTree.checkNode(treeNode, !treeNode.checked, null, true);
	return false;
}

function onCheck(e, treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
	nodes = zTree.getCheckedNodes(true);
	var	v = "";
	var ids = "";
	for (var i=0, l=nodes.length; i<l; i++) {
		if(i!=0){
			v+=",";
			ids +=",";
		}
		v += nodes[i].MODULE_NAME; 
		ids +=nodes[i].REC_ID; 
	}
	$("#citySel").attr("value", v);
	$("#moduleIds").attr("value", ids);
}

function showMenu() {
	var cityObj = $("#citySel");
	var cityOffset = $("#citySel").offset();
	$("#menuContent").css({}).slideDown("fast");

	$("body").bind("mousedown", onBodyDown);
}
function hideMenu() {
	$("#menuContent").fadeOut("fast");
	$("body").unbind("mousedown", onBodyDown);
}
function onBodyDown(event) {
	if (!(event.target.id == "menuBtn" || event.target.id == "citySel" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
		hideMenu();
	}
}
		
var zTree;  
var treeNodes;
$(function(){ 
    $.ajax({  
        async:false,  
        cache:false,  
        type: 'POST',  
        dataType : "json",  
        url: ctp + "/moduleController/moduleTree2.shtml",
        error: function () {
            alert('请求失败');  
        },  
        success:function(data){   
            treeNodes = data.list;   
            var treeObj = $.fn.zTree.init($("#treeDemo"), setting, treeNodes); 
            if(moduleIds!=null){
            	var s = moduleIds.split(",");
            	for(var i=0;i<s.length;i++){
            		 var node = treeObj.getNodeByParam("REC_ID", s[i], null);
            		 if(node!=null) {
                    	 if(node.isParent!=true) {                    		
                    		 treeObj.checkNode(node, true, true);	
                    	 }
                	}
            	}
           	 $("#citySel").attr("value", moduleNames);
        	 $("#moduleIds").attr("value", moduleIds);
            }            
        }  
    });  
  
});  
